import C_SwitchFrame from "./component/C_SwitchFrame";


const { ccclass, property, executeInEditMode } = cc._decorator;
enum ColorEnum {
    白色,
    红色,

}

enum HoleEnum {
    零,
    一个,
    两个,
}

const holeRadius = 15
@ccclass
@executeInEditMode
export default class C_HoleEditor extends cc.Component {
    @property({ editorOnly: true })
    _color: number = 0;

    @property({ type: cc.Enum(ColorEnum) })
    set color(v: number) {
        this._color = v;
        this.bg.frameIndex = v;
    }

    get color() {
        return this._color;
    }

    @property({ editorOnly: true })
    _size: cc.Size = new cc.Size(220, 40)
    @property()
    set size(v) {
        this._size = v;
        this.node.setContentSize(v);
        this.changePlate();
    }

    get size() {
        return this._size;
    }

    @property({ editorOnly: true })
    _holeNum: number = 0;
    @property({ type: cc.Enum(HoleEnum) })
    set holeNum(v) {
        Editor.log("holeNum:", v)
        this._holeNum = v;
        this.changePlate();
    }

    get holeNum() {
        return this._holeNum;
    }

    @property(C_SwitchFrame)
    bg: C_SwitchFrame = null;

    @property(cc.Prefab)
    holeNode: cc.Prefab = null;

    holeList: cc.Node[] = [];
    changePlate() {
        const spa = (this.size.width - 40) / this._holeNum;
        const half = (this.size.width - 40) / 2;
        for (let i = 0; i < this._holeNum; i++) {
            if (!this.holeList[i]) {
                this.holeList[i] = cc.instantiate(this.holeNode);

            }
            const hole = this.holeList[i];
            hole.parent = this.node;
            hole.active = true;
            // Editor.log("hole: ", i, half, holeRadius, spa, this._holeNum);
            if (this._holeNum == 1) {
                hole.x = 0;
            }
            else {
                const temp = (i % 2 ? -1 : 1);
                hole.x = spa * (Math.floor(i / 2) + 1) * temp + holeRadius * (-temp);
            }
        }

        for (let i = this._holeNum; i < this.holeList.length; i++) {
            const hole = this.holeList[i];
            hole.removeFromParent();
        }

        const colliders = this.node.getComponents(cc.PhysicsBoxCollider);
        for (let i = 0; i < colliders.length; i++) {
            this.node.removeComponent(colliders[i])
        }
        // 上下
        const top = this.node.addComponent(cc.PhysicsBoxCollider);
        const bottom = this.node.addComponent(cc.PhysicsBoxCollider);

        const height = top.size.height = bottom.size.height = this.size.height / 2 - holeRadius;
        const width = top.size.width = bottom.size.width = this.size.width;
        top.offset.y = holeRadius + height / 2;
        bottom.offset.y = -holeRadius - height / 2;
        // 左右
        const left = this.node.addComponent(cc.PhysicsBoxCollider);
        const right = this.node.addComponent(cc.PhysicsBoxCollider);
    }

    onLoad() {

        if (CC_EDITOR) {
            this.holeList = [];
            this.node.children.forEach(v => {
                this.holeList.push(v);
            })
        }
    }

}
